package sampleapp.client;

import java.util.List;
import java.util.logging.Logger;

import sampleapp.client.factory.PresenterWidgetFactory;
import sampleapp.client.place.NameTokens;
import sampleapp.shared.GetAllPeople;
import sampleapp.shared.GetAllPeopleResult;
import sampleapp.shared.PersonInfo;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.ManualRevealCallback;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;


public class PeoplePresenter extends
		Presenter<PeoplePresenter.MyView, PeoplePresenter.MyProxy> {

	public interface MyView extends View {
		// TODO Put your view methods here
	}
	
	
	public static final Object TYPE_PEOPLELIST = new Object();

	private static final Logger LOGGER = Logger.getLogger(PeoplePresenter.class.getName());

	private PresenterWidgetFactory<PersonWidgetPresenter> personWidgetPresenterFactory;
	
	@ProxyCodeSplit
	@NameToken(NameTokens.peoplepresenterpage)	
	public interface MyProxy extends ProxyPlace<PeoplePresenter> {
	}

    public void setPersonWIdgetFactory(PresenterWidgetFactory<PersonWidgetPresenter> personWidgetPresenterFactory){
    	this.personWidgetPresenterFactory=personWidgetPresenterFactory;
    }
	private DispatchAsync dispatcher;

	@Inject
	public PeoplePresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy, final DispatchAsync dispatcher, PresenterWidgetFactory<PersonWidgetPresenter> personWidgetPresenterFactory) {
		super(eventBus, view, proxy);
		this.dispatcher = dispatcher;
		this.personWidgetPresenterFactory  = personWidgetPresenterFactory;
	}

	@Override
	protected void revealInParent() {
		RevealRootContentEvent.fire(this, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
		doGetAllPeople();
	}
	
	public void doGetAllPeople(){
		
		final PeoplePresenter thisPresenter = this;	
		
		dispatcher.execute(new GetAllPeople(),
		
			ManualRevealCallback.create(this,
					new AsyncCallback<GetAllPeopleResult>() {

						@Override
						public void onFailure(Throwable caught) {
							// Display an error message
						
							System.out.println("major error");
						}

						@Override
						public void onSuccess(
								GetAllPeopleResult result) {
							List<PersonInfo> lstPersonInfo = result
									.getPersonInfo();
                            System.out.println("Records returned from server " + lstPersonInfo.size());
							for (final PersonInfo info : lstPersonInfo) {

								System.out.println("Record from Server"
										+ info.getFirstName());

								
								PersonWidgetPresenter personWidgetPresenter=personWidgetPresenterFactory.create();
								if (personWidgetPresenter==null){
									System.out.println("CREATE RETURNED A NULL");
								}
								
								personWidgetPresenter.setPersonInfo(info);
								if (thisPresenter==null){
									System.out.println("This presenter is null");
								}
								thisPresenter.addToSlot(PeoplePresenter.TYPE_PEOPLELIST,
										personWidgetPresenter);
								
										

								

							}
							
						}
					}));
			
			
	
	}
}
